package com.lp.controller.iot;

import com.lp.bo.IotAlarmInfoBO;
import com.lp.bo.UserInfoBO;
import com.lp.common.Code;
import com.lp.common.CodeIot;
import com.lp.common.Constants;
import com.lp.common.RequestURLIOT;
import com.lp.controller.BaseController;
import com.lp.util.DateUtils;
import com.lp.util.ObjectUtil;
import com.lp.util.ResultMapUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.util.Map;


@Controller
public class IotAlarmInfoController extends BaseController {

	/**
	 * 检索
	 * @param response
	 * @param 
	 * @return
	 */
	@RequestMapping(method = RequestMethod.POST, value = RequestURLIOT.AlarmInfo.ALARM_INFO_PAGE)
	public ModelAndView selectPage(HttpServletResponse response, 
			@RequestHeader(value = ResultMapUtils.USER_KEY, required = true) String userKey ,
			@RequestBody IotAlarmInfoBO obj,
			@RequestParam(required=false) Integer pageSize ,
			@RequestParam Integer paged ) {
		Map<String, Object> resultMap = getResultMap();
		try {
			UserInfoBO user = getUserInfoByUserKey(userKey);
			obj.setUser_id(user.getId());
			resultMap = service.selectPageList("IotAlarmInfo.selectPage",getPageBean(paged,pageSize), obj);
//			List<IotAlarmInfo> list = new ArrayList<>();
//			String serverIp = "192.168.1.7"; // 目标IP地址
//			int serverPort = 20108; // 目标端口
//			String message = "8613196660066:0:你好，Hello，欢迎使用通信的模块。"; // 要发送的消息
//
//			try (Socket socket = new Socket(serverIp, serverPort)) {
//				System.out.println("Connected to server at " + serverIp + ":" + serverPort);
//
//				// 获取输出流
//				OutputStream outputStream = socket.getOutputStream();
//
//				// 发送数据
//				outputStream.write(message.getBytes("GBK"));
//				outputStream.flush();
//				outputStream.close();
//
//				System.out.println("Message sent: " + message);
//			} catch (IOException e) {
//				e.printStackTrace();
//			}
		} catch (Exception e) {
			exception(e,resultMap, obj);
		}
		return getModelAndView(response, resultMap);
	}
	
	/**
	 * 报警统计
	 * @param response
	 * @param 
	 * @return
	 */
	@RequestMapping(method = RequestMethod.POST, value = RequestURLIOT.AlarmInfo.ALARM_INFO_STATISTIC)
	public ModelAndView selectAlarmStatisticInfo(HttpServletResponse response, 
			@RequestHeader(value = ResultMapUtils.USER_KEY, required = true) String userKey ,
			@RequestBody IotAlarmInfoBO obj ) {
		Map<String, Object> resultMap = getResultMap();
		try {
			UserInfoBO user = getUserInfoByUserKey(userKey);
			obj.setUser_id(user.getId());
			if(ObjectUtil.isEmpty(obj.getStart_time())){
				obj.setStart_time( DateUtils.format(DateUtils.simpleALL, DateUtils.getDate(5))  );
			}
			resultMap = service.selectList("IotAlarmInfo.selectStatisticInfo", obj );	
		} catch (Exception e) {
			exception(e,resultMap, obj);
		}
		return getModelAndView(response, resultMap);
	}
	
	/**
	 * Excel 下载
	 * @param response
	 * @param obj
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET, value = RequestURLIOT.AlarmInfo.ALARM_INFO_EXCEL )
	public void excel(HttpServletResponse response, 
			 IotAlarmInfoBO obj ) {
		Map<String, Object> resultMap = getResultMap();
		try {
			downExcel("IotAlarmInfo.select", "报警信息下载", "tpl/xls/alarm_info_template", obj, response);
		} catch (Exception e) {
			exception(e,resultMap, obj);
		}
	}
	
	
	/**
	 * 插入
	 * @param response
	 * @param 
	 * @return
	 */
	@RequestMapping(method = RequestMethod.POST, value = RequestURLIOT.AlarmInfo.ALARM_INFO )
	public ModelAndView save(HttpServletResponse response, 
			@RequestHeader(value = ResultMapUtils.USER_KEY, required = true) String userKey ,
			@RequestBody IotAlarmInfoBO obj ) {
		Map<String, Object> resultMap = getResultMap();
		try {
			resultMap = service.insert("IotAlarmInfo.insert", obj) ;
		} catch (Exception e) {
			exception(e,resultMap, obj);
		}
		return getModelAndView(response, resultMap);
	}
	
	/**
	 * 查询单个
	 * @param response
	 * @param id
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET, value = RequestURLIOT.AlarmInfo.ALARM_INFO)
	public ModelAndView selectOne(HttpServletResponse response, 
			@RequestHeader(value = ResultMapUtils.USER_KEY, required = true) String userKey ,
			@RequestParam Integer id ) {
		Map<String, Object> resultMap = getResultMap();
		try {
			UserInfoBO user = getUserInfoByUserKey(userKey);
			IotAlarmInfoBO obj =new IotAlarmInfoBO(id);
			obj.setUser_id(user.getId());
			resultMap = service.selectOne("IotAlarmInfo.selectOne", obj) ;
		} catch (Exception e) {
			exception(e,resultMap, id);
		}
		return getModelAndView(response, resultMap);
	}
	
	/**
	 * 标记所有为已读
	 * @param response
	 * @param userKey
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET, value = RequestURLIOT.AlarmInfo.ALARM_INFO_READ)
	public ModelAndView allRead(HttpServletResponse response, 
			@RequestHeader(value = ResultMapUtils.USER_KEY, required = true) String userKey
			) {
		Map<String, Object> resultMap = getResultMap();
		try {
			IotAlarmInfoBO obj = new IotAlarmInfoBO();
			UserInfoBO user = getUserInfoByUserKey(userKey);
			obj.setUser_id(user.getId());
			obj.setIot_alarm_process_status(CodeIot.PROCESS_STATUS.YES);
			resultMap = service.selectOne("IotAlarmInfo.setMyAllread", obj) ;
		} catch (Exception e) {
			exception(e,resultMap);
		}
		return getModelAndView(response, resultMap);
	}
	
	/**
	 * 更新
	 * @param response
	 * @param obj
	 * @return
	 */
	@RequestMapping(method = RequestMethod.PUT, value = RequestURLIOT.AlarmInfo.ALARM_INFO)
	public ModelAndView update(HttpServletResponse response, 
			@RequestHeader(value = ResultMapUtils.USER_KEY, required = true) String userKey ,
			@RequestBody IotAlarmInfoBO obj ) {
		Map<String, Object> resultMap = getResultMap();
		try {
			UserInfoBO user = getUserInfoByUserKey(userKey);
			obj.setUser_id(user.getId());
			resultMap = service.update("IotAlarmInfo.update", obj) ;
		} catch (Exception e) {
			exception(e,resultMap, obj);
		}
		return getModelAndView(response, resultMap);
	}
	
	/**
	 * 删除
	 * @param response
	 * @param id
	 * @return
	 */
	@RequestMapping(method = RequestMethod.DELETE, value = RequestURLIOT.AlarmInfo.ALARM_INFO)
	public ModelAndView delete(HttpServletResponse response, 
			@RequestHeader(value = ResultMapUtils.USER_KEY, required = true) String userKey ,
			@RequestParam Integer id ) {
		Map<String, Object> resultMap = getResultMap();
		try {
			IotAlarmInfoBO obj = new IotAlarmInfoBO();
			if(ObjectUtil.isEmpty(id)){
				putStatusCode(resultMap, Code.ResponseCode.SystemCode.PARAM_ERROR);
			}else{
				UserInfoBO user = getUserInfoByUserKey(userKey);
				obj.setUser_id(user.getId());
				obj.setId(id);
				obj.setDelete_flag(Constants.DELETE.YES);
				resultMap = service.update("IotAlarmInfo.update", obj) ;		
			}
		} catch (Exception e) {
			exception(e,resultMap, id);
		}
		return getModelAndView(response, resultMap);
	}
	
	/**
	 * 查询(未读报警)的数量
	 * @param response
	 * @param id
	 * @return
	 */
	@RequestMapping(method = RequestMethod.POST, value = RequestURLIOT.AlarmInfo.ALARM_INFO_UNREAD)
	public ModelAndView selectUnreadCount(HttpServletResponse response, 
			@RequestHeader(value = ResultMapUtils.USER_KEY, required = true) String userKey,
			@RequestBody IotAlarmInfoBO obj) {
		Map<String, Object> resultMap = getResultMap();
		try {
			UserInfoBO user = getUserInfoByUserKey(userKey);
			obj.setUser_id(user.getId());
			Integer count = service.count("IotAlarmInfo.selectPageCount", obj);
			resultMap.put("count", count);
		} catch (Exception e) {
			exception(e,resultMap);
		}
		return getModelAndView(response, resultMap);
	}
	
	
    /**
     * 查询所有报警（大屏统计）
     *
     * @param response
     * @param userKey
     * @param obj
     * @return
     */
    @RequestMapping(method = RequestMethod.POST, value = RequestURLIOT.AlarmInfo.ALARM_INFO_ALL)
    public ModelAndView selectAll(HttpServletResponse response,
                                  @RequestHeader(value = ResultMapUtils.USER_KEY, required = true) String userKey,
                                  @RequestBody IotAlarmInfoBO obj) {
        Map<String, Object> resultMap = getResultMap();
        try {
            UserInfoBO user = getUserInfoByUserKey(userKey);
            obj.setUser_id(user.getId());
            resultMap = service.selectList("IotAlarmInfo.selectAll", obj);

        } catch (Exception e) {
            exception(e, resultMap, obj);
        }
        return getModelAndView(response, resultMap);
    }

	
}
